package com.sk.commonadapter_android.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author yemao
 * @Email yrmao9893@163.com
 * @Date 2017/6/12
 * @Des 通用RecyclerViewAdapter基类!
 */

public abstract class CommonRecyclerViewAdapter<DATA> extends RecyclerView.Adapter<CommonViewHolder> {

    protected List<DATA> mData;
    protected int mLayoutId;
    protected Context mContext;
    public CommonRecyclerViewAdapter(Context mContext, @LayoutRes int mLayoutId){
        this(mContext, mLayoutId,new ArrayList<DATA>());
    }
    public CommonRecyclerViewAdapter(Context mContext, @LayoutRes int mLayoutId, List<DATA> mData){
        this.mContext=mContext;
        this.mLayoutId=mLayoutId;
        this.mData=(mData==null?new ArrayList<DATA>():mData);

    }

    @Override
    public CommonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new CommonViewHolder(LayoutInflater.from(mContext).inflate(mLayoutId,null));
    }

    @Override
    public void onBindViewHolder(CommonViewHolder holder, int position) {

        convert(holder, mData.get(position), position);

    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }
    public void setNewData(){

    }
    public void add(DATA data){
        mData.add(data);
    }
    public void addAll(List<DATA> datas){
        mData.addAll(datas);
    }
    public void removeItem(int position){
        mData.remove(position);
    }
    public void clear(){
        mData.clear();
    }

    public abstract void convert(CommonViewHolder holder, DATA item, int position);

}